//
//  DXTabBarController.swift
//  新浪微博
//
//  Created by dix on 2017/1/16.
//  Copyright © 2017年 dix. All rights reserved.
//

import UIKit

class DXTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        let tabBar = DXTabBar()
        //2 实例化闭包
        tabBar.closure = {
        //4 闭包回调
        
        }
        //通过kvc给只读属性赋值
        setValue(tabBar, forKey: "tabBar")
          ///首页
        addChildViewController(DXHomeViewController(), title: "首页", imgName: "tabbar_home")
        //消息
        addChildViewController(DXMessageViewController(), title: "消息", imgName: "tabbar_message_center")
        //发现
        addChildViewController(DXDiscoverViewController(), title: "发现", imgName: "tabbar_discover")
        //我
        addChildViewController(DXProfileViewController(), title: "我", imgName: "tabbar_profile")
        
    }
    
}
extension DXTabBarController{
    /// 添加子控制器的公共方法时
    ///
    /// - Parameters:
    ///   - childController: 控制器
    ///   - title: 名字
    ///   - imgName: 图片名称
    func addChildViewController(_ childController: UIViewController, title : String,imgName : String) {
        //设置title
        childController.title = title
        //设置title颜色
        childController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : DXThemeColor], for: UIControlState.selected)
        // 设置image
        // withRenderingMode 渲染方式
        // alwaysOriginal 采用图片原生颜色
        childController.tabBarItem.image = UIImage(named: imgName)?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        childController.tabBarItem.selectedImage = UIImage(named: "\(imgName)_selected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        //添加子控制器
        addChildViewController(DXNavigationController(rootViewController: childController))
    }
}
